//
//  VSBaseDaoRemote.m
//  Vstore
//
//  Created by Eric Scott on 10/31/14.
//  Copyright (c) 2014 LITB. All rights reserved.
//

#import "VSHttpNetworking.h"
#import "VSHttpRequest.h"

#import "AFHTTPRequestOperationManager+RACSignalSupport.h"
#import "AFHTTPRequestOperationManager.h"


@interface VSHttpNetworking ()

@property (nonatomic, strong) AFHTTPRequestOperationManager * httpManager;

@end


@implementation VSHttpNetworking

MACRO_SHARED_INSTANCE_IMPLEMENTATION(VSHttpNetworking);

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.httpManager = [[AFHTTPRequestOperationManager alloc] init];
        self.httpManager.requestSerializer = [AFJSONRequestSerializer serializer];
        self.httpManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"text/plain", @"application/json", @"text/javascript", nil];
    }
    return self;
}

- (RACSignal *)rac_signalForHttpGET:(NSString *)URLString
                         parameters:(NSDictionary *)parameters
{
    SAFE_PREFORM_SELECTOR_WITH_OBJECT(self.delegate, @selector(willSendRequest:), URLString);
    return [self.httpManager rac_signalForGET:URLString
                                   parameters:parameters];
}

- (RACSignal *)rac_signalForHttpPOST:(NSString *)URLString
                          parameters:(NSDictionary *)parameters
{
    SAFE_PREFORM_SELECTOR_WITH_OBJECT(self.delegate, @selector(willSendRequest:), URLString);
    return [self.httpManager rac_signalForPOST:URLString
                                    parameters:parameters];
}

- (RACSignal *)rac_signalForHttpPOSTWithoutJson:(NSString *)URLString
                                     parameters:(NSDictionary *)parameters
{
    SAFE_PREFORM_SELECTOR_WITH_OBJECT(self.delegate, @selector(willSendRequest:), URLString);
    return [self.httpManager rac_signalForPOSTWithoutJson:URLString
                                               parameters:parameters];
}

- (RACSignal *)rac_signalForHttpDELETE:(NSString *)URLString
                            parameters:(NSDictionary *)parameters
{
    SAFE_PREFORM_SELECTOR_WITH_OBJECT(self.delegate, @selector(willSendRequest:), URLString);
    return [self.httpManager rac_signalForDELETE:URLString
                                      parameters:parameters];
}

- (RACSignal *)rac_signalForHttpPOST:(NSString *)URLString
                          parameters:(NSDictionary *)parameters
                            filePath:(NSURL *)filePath
                                name:(NSString *)name
                            fileName:(NSString *)fileName
                            mimeType:(NSString *)mimeType;
{
    SAFE_PREFORM_SELECTOR_WITH_OBJECT(self.delegate, @selector(willSendRequest:), URLString);
    return [self.httpManager rac_signalForPOST:URLString
                                    parameters:parameters
                                      filePath:filePath
                                          name:name
                                      fileName:fileName
                                      mimeType:mimeType];
}

- (RACSignal *)rac_signalForHttpPOST:(NSString *)URLString
                          parameters:(NSDictionary *)parameters
                            fileData:(NSData *)fileData
                                name:(NSString *)name
                            fileName:(NSString *)fileName
                            mimeType:(NSString *)mimeType;
{
    SAFE_PREFORM_SELECTOR_WITH_OBJECT(self.delegate, @selector(willSendRequest:), URLString);
    return [self.httpManager rac_signalForPOST:URLString
                                    parameters:parameters
                                      fileData:fileData
                                          name:name
                                      fileName:fileName
                                      mimeType:mimeType];
}

@end
